<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace app\filters;

use app\models\UserLogin;
use Yii;
use yii\base\ActionEvent;
use yii\base\Behavior;
use yii\web\Controller;
use yii\web\ForbiddenHttpException;
use yii\helpers\Url;

class AccessFilter extends Behavior {

    public $actions = [];

    //------------------------------------------------------------------------
    //本站用户
    const MODULE_UUID_USER = 'e36b9506-1821-4463-9fa3-74c7a1af23f3';
    //监控
    const MODULE_UUID_JIANKONG = 'c0613f12-067c-4265-a7f2-b09d6acdfbd5';
    //直播记录
    const MODULE_UUID_LIVE_RECORD = '6c7cd512-6250-4d6d-b83b-31f5cf288e30';
    //直播分类管理
    const MODULE_UUID_LIVE_CATE = '9ec22f7b-fa79-4194-86dc-fe66cdb68c7c';
    //视频列表
    const MODULE_UUID_VIDEO_LIST = 'e71185cf-cd7c-43ff-bf31-8b70e4e1b37c';
    //首页推荐列表
    const MODULE_UUID_VIDEO_RECOMMEND = '86ed36ef-3a51-4f97-99b3-11d61b336bce';

    //首页推荐列表
    const MODULE_UUID_GIFT = 'a6e56320-c661-413e-9de5-0a8e71e7eff8';

    //首页推荐列表
    const MODULE_UUID_REPORT = 'e47bdfb3-c46b-40d2-9ad5-e0b38eb6e2fb';

    //订单列表
    const MODULE_UUID_ORDER = 'ad9abe48-7f20-455e-8364-4b155a76a0e0';

    //系统设置
    const MODULE_UUID_MENU = 'c0c976e1-1a6f-4c4b-b30a-b7a68d194fa4';

    //------------------------------------------------------------------------
    // 通用 菜单
    // const MODULE_UUID_LANG_TPL        = '62690f2e-9494-45bd-a627-7dc6807ed04c'; // 语言KEY模板
    // const MODULE_UUID_LANG_ITEM       = '91889ce8-b11a-4527-9c73-9cb1c7ac8860'; // 语言KEY赋值
    // const MODULE_UUID_LANG_LIST       = '2620f9ce-8a75-4c1b-ab36-5fc901092e2e'; // 语言列表
    // const MODULE_UUID_MENU            = '62aeae00-9e28-44d6-99e5-34662253f6ff'; // 菜单管理
    // const MODULE_UUID_ACTIVITY_CONFIG = 'cd429587-28bc-4787-a878-78339740a0e3'; // 活动管理
    // const MODULE_UUID_LOG             = 'f0637ef4-b46a-43d7-9552-0a4f989458cc'; // 日志管理

    static $ACTION_RELATION = [
        //------------------------------------------------------------------------
        // 通用操作
        // 此处中文不会在界面显示.判断权限只会isset判断.此处中文只是为了看的更清楚一点],
        // self::MODULE_UUID_MENU            => ['add' => '增加', 'del' => '删除', 'edit' => '编辑', 'list' => '显示'],
        // self::MODULE_UUID_LANG_TPL        => ['add' => '新增', 'del' => '删除', 'edit' => '编辑', 'list' => '列表'],
        // self::MODULE_UUID_LOG             => ['list' => '列表'],
        // self::MODULE_UUID_LANG_ITEM       => ['edit' => '编辑', 'list' => '列表'],
        // self::MODULE_UUID_LANG_LIST       => ['add' => '新增', 'del' => '删除', 'edit' => '编辑', 'list' => '列表'],
        // self::MODULE_UUID_ACTIVITY_CONFIG => ['add' => '新增', 'del' => '删除', 'edit' => '编辑', 'list' => '列表'],
        // self::MODULE_UUID_GAME            => ['query' => '查询'],

        //------------------------------------------------------------------------
        // 美美
        // self::MODULE_UUID_ACTIVITY        => ['add' => '新增', 'choose_server' => '选服', 'del' => '删除', 'edit' => '编辑', 'list' => '查看', 'offline' => '下线', 'online' => '上线'],

        // 广告
//        self::MODULE_UUID_CONFIG          => ['add' => '新增', 'choose_server' => '选服', 'del' => '删除', 'edit' => '编辑', 'list' => '查看', 'offline' => '下线', 'online' => '上线'],
    ];

    //------------------------------------------------------------------------
    // liberators所需权限

    /**
     * Declares event handlers for the [[owner]]'s events.
     * @return array events (array keys) and the corresponding event handler methods (array values).
     */
    public function events() {
        return [Controller::EVENT_BEFORE_ACTION => 'beforeAction'];
    }

    /**
     * @param ActionEvent $event
     * @return boolean
     * @throws MethodNotAllowedHttpException when the request method is not allowed.
     */
    public function beforeAction($event) {
        if (!isset($this->actions[$event->action->actionMethod]) && !isset($this->actions['*'])) {
            return true;
        }

        //不在权限里面直接判断是否管理员.为了统一API里的模块权限数据.API没有管理员概念
        $authedData = UserLogin::getLoginedModel()->authedData;
         
        $moduleUUID = isset($this->actions[$event->action->actionMethod]) ? $this->actions[$event->action->actionMethod][0] : $this->actions['*'][0];
        $actionName = isset($this->actions[$event->action->actionMethod]) ? $this->actions[$event->action->actionMethod][1] : $this->actions['*'][1];
        if (isset($authedData[$moduleUUID][$actionName]) || (string) $moduleUUID === '') {
            return true;
        }

        //自动判断是否为ajax请求，ajax跳转 header头里面加X-Redirect
        exit(Yii::$app->response->redirect(
            Url::to(['index/access-error']).'?url=' . urlencode(Yii::$app->request->getReferrer()).'&errorCode=403',
            302
        )->send());
    }

    /**
     * 验证模块
     *
     * @access public
     * @param  void   $moduleUUID
     * @param  void   $actionName
     * @return void
     */
    public static function checkAuth($moduleUUID, $actionName) {
        $authedData = UserLogin::getLoginedModel()->authedData;

        return (isset($authedData[$moduleUUID][$actionName]) || (string) $moduleUUID === '') ? true : false;
    }
}
